package net.huashitong.ssydt.provider.article.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.huashitong.ssydt.article.entity.vo.*;
import net.huashitong.ssydt.provider.annotations.LoginRequired;
import net.huashitong.ssydt.provider.article.service.ArticleService;
import net.huashitong.ssydt.provider.web.model.Result;
import net.huashitong.ssydt.provider.web.utils.ResultUtils;
import net.huashitong.ssydt.security.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2017.09.14
 */
@Api(value = "/api/article/", tags = {"文章模块"})
@RestController
@RequestMapping("/api/article/")
public class ApiArticleController {

    private ArticleService articleService;

    @Autowired
    public ApiArticleController(ArticleService articleService) {
        this.articleService = articleService;
    }

    @ApiOperation(value = "文章列表", notes = "文章列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/articles")
    public Result<ArrayList<ArticleListOutVO>> getArticles(ArticleListInVO articleListInVO) {
        List<ArticleListOutVO> articleListOutVOS = articleService.getArticles(articleListInVO);
        return ResultUtils.getSuccessResultList(articleListOutVOS);
    }

    @ApiOperation(value = "文章分类列表", notes = "文章分类列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/article-types")
    public Result<ArrayList<ArticleTypeListVO>> getArticleTypes() {
        List<ArticleTypeListVO> articleListOutVOS = articleService.getArticleTypes();
        return ResultUtils.getSuccessResultList(articleListOutVOS);
    }

    @ApiOperation(value = "文章详情底部信息", notes = "文章详情底部信息", produces = MediaType.APPLICATION_JSON_VALUE)
    @GetMapping("/articles/{articleId}/bottom")
    public Result<ArticleBottomVO> getArticleBottom(
            @ApiParam(value = "文章id", required = true) @PathVariable("articleId") Long articleId) {
        String userId = UserUtils.getUserId();
        ArticleBottomVO articleBottomVO = articleService.getArticleBottom(articleId, userId);
        return ResultUtils.getSuccessResults(articleBottomVO);
    }

    @ApiOperation(value = "执行文章点赞", notes = "执行文章点赞", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @PostMapping("/articles/{articleId}/praises")
    public Result<String> postArticlePraises(
            @ApiParam(value = "文章id", required = true) @PathVariable("articleId") Long articleId) {
        String userId = UserUtils.getUserId();
        articleService.postArticlePraises(articleId, userId);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "取消文章点赞", notes = "取消文章点赞", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @DeleteMapping("/articles/{articleId}/praises")
    public Result<String> deleteArticlePraises(
            @ApiParam(value = "文章id", required = true) @PathVariable("articleId") Long articleId) {
        String userId = UserUtils.getUserId();
        articleService.deleteArticlePraises(articleId, userId);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "执行文章收藏", notes = "执行文章收藏", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @PostMapping("/articles/{articleId}/collections")
    public Result<String> postArticleCollections(
            @ApiParam(value = "文章id", required = true) @PathVariable("articleId") Long articleId) {
        String userId = UserUtils.getUserId();
        articleService.postArticleCollections(articleId, userId);
        return ResultUtils.getSuccessResults();
    }

    @ApiOperation(value = "取消文章收藏", notes = "取消文章收藏", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @DeleteMapping("/articles/{articleId}/collections")
    public Result<String> deleteArticleCollections(
            @ApiParam(value = "文章id", required = true) @PathVariable("articleId") Long articleId) {
        String userId = UserUtils.getUserId();
        articleService.deleteArticleCollections(articleId, userId);
        return ResultUtils.getSuccessResults();
    }


    @ApiOperation(value = "清空文章收藏", notes = "清空文章收藏", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @DeleteMapping("/api/article/articles/collections")
    public Result<String> deleteArticlesCollections() {
        String userId = UserUtils.getUserId();
        articleService.deleteArticlesCollections(userId);
        return ResultUtils.getSuccessResults();
    }


    @ApiOperation(value = "文章收藏列表", notes = "文章收藏列表", produces = MediaType.APPLICATION_JSON_VALUE)
    @LoginRequired
    @GetMapping("/api/article/articles/collections")
    public Result<ArrayList<ArticleListOutVO>> getArticlesCollections(CollectionsQueryVO collectionsQueryVO) {
        String userId = UserUtils.getUserId();
        collectionsQueryVO.setUserId(userId);
        List<ArticleListOutVO> articleListOutVOS = articleService.getArticlesCollections(collectionsQueryVO);
        return ResultUtils.getSuccessResultList(articleListOutVOS);
    }


}
